
;(add-hook 'sql-interactive-mode-hook 'sql-ms-triggers-helper-mode-provide);can't do this, because stuff starts to fight for posession of the hook

(defun sql-ms-triggers-helper-mode-provide ()
  ;;some basic setup
  (defconst sql-ms-helper-version "0.0")
  (defvar sql-ms-triggers-helper-mode nil)
  (defvar sql-ms-triggers-helper-mode-map nil)


  (defvar sql-ms-helper-toggle-hook 'sql-ms-triggers-helper-mode-toggle)
					;(add-to-list 'auto-mode-alist '("\\.timer\\'" . sql-ms-triggers-helper-mode))



  (unless sql-ms-triggers-helper-mode-map
    (setq sql-ms-triggers-helper-mode-map (make-sparse-keymap))
    (define-key sql-ms-triggers-helper-mode-map "\M-\r" 'ms-sql-helper-trigger-process))

  (defun sql-ms-triggers-helper-mode-toggle ()
    (if sql-ms-triggers-helper-mode ;sql-ms-triggers-helper-mode gets toggled when activating and de-activating the mode. SUPER
	(progn 
	  (message (concat "sql-ms-triggers-helper-mode activated" ))
	  (setf *this-window* (selected-window))
	  (setf *window-below* (next-window))
	  (setf *search-str* nil)
	  (setf *proc-name* "")
	  (setf *current-proc* "")
	  (setf *line-number* 0)
	  (setf *current-indentation* 0)
	  (setf *processed-hash* (make-hash-table :test 'equal))
	  (global-set-key '[M-left] 'ms-sql-helper-goto-other-buffer)



	  
	  (defun ms-sql-helper-set-search-name (str)
	    (interactive)
	    (setf *search-str* str)) ;for now, for hard coding, this is okay.

	  (ms-sql-helper-set-search-name "tU_")
	  

	  (defun ms-sql-helper-matches-to-list (str)
	    (let ((mypoint 0)
		  (safetycount 0)
		  (lst '()))
	      (while (and (numberp mypoint) (< safetycount 100))
		(setf mypoint (search-forward str nil t))
		(incf safetycount)
		(if (and (numberp mypoint) (> (- mypoint (length str)) 0))
		    (progn
;		      (beginning-of-line)
		      (setf mystart (point))
		      ;(search-forward "\"")
		      (search-forward " ")
		      ;(skip-syntax-forward "w_")
		      ;   (skip-syntax-forward "w_")
;		      (end-of-line)
		      (setf myend (point))
		      (setf whole-match (buffer-substring-no-properties mystart myend))
		      ;(push (concat "line: " (number-to-string (line-number-at-pos)) whole-match) lst))))
		      ;lose the inverted commas and spaces
		      
		      (push  (sql-ms-query-parse-chomp (replace-regexp-in-string "\"" "" whole-match)) lst))))
	      lst))
	  
	  (defun ms-sql-helper-place-list-below-this-line (lst)
	    (setf mypointnow (point))
	    (while (> (length lst) 0)
	      (setf mymu (pop lst))
	      (end-of-line)
	      (newline)
	      (indent-to-column *current-indentation*) ;sheesh. Should be replaced with something that 
	      (insert "+-- ")
	      (insert mymu))
	    (goto-char mypointnow))

	  (defun ms-sql-helper-search-for-matches-in-other-buffer (other-window this-window match-string)
	    (select-window other-window)
	    (ms-sql-helper-kill-blank-lines)
	    (beginning-of-buffer)
	    (setf my-matches '())
	    (setf my-matches (ms-sql-helper-matches-to-list (concat "\"" match-string "\"" " because \"")))
	    (select-window this-window)
	    my-matches)


	  (defun ms-sql-helper-run-query (qstr)
					;(interactive)
					;(message (concat "QUERY: " qstr))
	    (sql-send-string qstr)
	    (sql-send-string "go"))


	  (defun ms-sql-helper-clear-other-window (other-window this-window)
	    (select-window other-window)
	    (delete-region (point-min) (point-max))
	    (select-window this-window))

	  (defun ms-sql-helper-run-bogus ()
	    (insert " ------- to freakin' infinity!"))
	  
	  (defun ms-sql-helper-run (table-name context)
					;(interactive)
	    (puthash table-name t *processed-hash*)
	    (ms-sql-helper-clear-other-window *window-below* *this-window*)
	    (ms-sql-helper-run-query (concat "exec sp_helptext " context table-name))
	    (setf *this-window* (selected-window))
	    (setf *window-below* (next-window))
	    (setf point-max 0)
	    (setf point-delta 1)
	    (setf safetycount 1)
	    (while (and (> point-delta 0) (< safetycount 60))
	      (setf point-now (ms-sql-helper-max-point *window-below* *this-window*))
	      (setf point-delta (- point-now point-max))
	      (setf point-max point-now)
	      (incf safetycount)
	      (sleep-for 1))
	    (setf all-matches (ms-sql-helper-search-for-matches-in-other-buffer *window-below* *this-window* table-name))
	    (setf all-matches (nreverse all-matches))
	    (ms-sql-helper-place-list-below-this-line all-matches)
	    (ms-sql-helper-clear-other-window *window-below* *this-window*))

	  (defun ms-sql-helper-max-point (window-below this-window)
	    (select-window window-below)
	    (setf mymaxpoint (point-max))
	    (select-window this-window)
	    mymaxpoint)


					;okay, so when we're on a line, we need to be able to go to the line to get the context of everything.
	  (defun ms-sql-helper-goto-point-in-proc ()
	    (interactive)
	    (setf mypointnow (point))
	    (skip-syntax-backward "w_")
	    (setf mystart (point))
	    (skip-syntax-forward "w_")
	    (setf myend (point))
	    (setf whole-match (buffer-substring-no-properties mystart myend))
	    (message (concat "line->" whole-match))
	    (setf line-number (string-to-number whole-match))
	    (message (concat "LINE->" (number-to-string line-number)))
	    (search-backward ">>")	;so we get to the marker
	    (re-search-forward "[a-zA-Z0-9_]+" nil t)
	    (skip-syntax-backward "w_")
	    (setf mystart (point))
	    (skip-syntax-forward "w_")
	    (setf myend (point))
	    (goto-char mypointnow)
	    (setf whole-match (buffer-substring-no-properties mystart myend))
	    (setf proc-name whole-match)
	    (setf point-max 0)
	    (setf point-delta 1)
	    (setf safetycount 1)
	    (if (equal *current-proc* proc-name)
		(progn
		  (message "samesame")
		  (select-window *window-below*)
		  (ms-sql-helper-kill-blank-lines)
		  (goto-line line-number)
		  )
	      (progn
		(ms-sql-helper-clear-other-window *window-below* *this-window*)
		(setf *current-proc* proc-name)
		(ms-sql-helper-run-query (concat "exec sp_helptext " proc-name))
		(while (and (> point-delta 0) (< safetycount 60))
		  (setf point-now (ms-sql-helper-max-point *window-below* *this-window*))
		  (setf point-delta (- point-now point-max))
		  (setf point-max point-now)
		  (incf safetycount)
		  (sleep-for 1))
		(select-window *window-below*)
		(ms-sql-helper-kill-blank-lines)
		(goto-line line-number))))

	  (defun ms-sql-helper-goto-other-buffer ()
	    (interactive)
	    (select-window *this-window*))

	  (defun ms-sql-helper-trigger-process ()
	    (interactive)
	    ;(setf *current-indentation* (current-indentation))
	    (skip-syntax-backward "w_")
	    (setf *current-indentation* (current-column))
	    ;(insert ">> ")
	    (setf mystart (point))
	    (skip-syntax-forward "w_")
	    (setf myend (point))
	    (setf table-name (buffer-substring-no-properties mystart myend))
	    ;(setf ret '())
	      ;(message (concat "---->" table-name (gethash table-name *processed-hash*)))
	      (if (gethash table-name *processed-hash*)
		  (progn
		    (message "done BEFORE")
		    (ms-sql-helper-run-bogus)
		  		    )
		(progn
		  (message "NOT DONE BEFORE")
		  (message (concat "calling " table-name " " *search-str*))
		  (ms-sql-helper-run table-name *search-str*)
		  )))

	  (defun ms-sql-helper-kill-blank-lines ()
	    (interactive)
	    (end-of-buffer)
	    (while (re-search-backward "^[ ]+$" nil t)
	      (beginning-of-line)
	      (kill-line)))

(defun sql-ms-query-parse-chomp (str)
  "This function has been stolen off the web
I think the right person should get credit for it
but I can't for the LIFE of me remember where I found it..."
  (let ((s (if (symbolp str)(symbol-name str) str)))
    (save-excursion
      (while (and
	      (not (null (string-match "^\\( \\|\f\\|\t\\|\n\\)" s)))
	      (> (length s) (string-match "^\\( \\|\f\\|\t\\|\n\\)" s)))
	(setq s (replace-match "" t nil s)))
      (while (and
	      (not (null (string-match "\\( \\|\f\\|\t\\|\n\\)$" s)))
	      (> (length s) (string-match "\\( \\|\f\\|\t\\|\n\\)$" s)))
	(setq s (replace-match "" t nil s))))
    s))


	  
	  )
      (progn
	(global-set-key '[M-left] 'backward-word)
	(setq sql-ms-triggers-helper-mode-map nil)
	(defun ms-sql-helper-kill-blank-lines () nil)
	(defun ms-sql-helper-trigger-process () nil)
	(defun ms-sql-helper-goto-other-buffer () nil)
	(defun ms-sql-helper-goto-point-in-proc () nil)
	(defun ms-sql-helper-set-search-name () nil)
	(defun sql-ms-query-parse-chomp () nil)
	(message (concat "sql-ms-triggers-helper-mode deactivated" )))))

 
  (define-minor-mode sql-ms-triggers-helper-mode
    "Toggle Timer mode"
    nil " timer" sql-ms-triggers-helper-mode-map
    (run-hooks 'sql-ms-helper-toggle-hook)) ;you don't seem to need to run the hooks, if the naming is nice, like if you'd called it sql-ms-triggers-helper-mode-hook.
  
  (provide 'sql-ms-triggers-helper-mode))
;;;enable everything
(sql-ms-triggers-helper-mode-provide)